import formInfo from '@ohos.app.form.formInfo';
import formBindingData from '@ohos.app.form.formBindingData';
import FormExtensionAbility from '@ohos.app.form.FormExtensionAbility';
import common from '@ohos.app.ability.common'
import formProvider from '@ohos.app.form.formProvider';
import { CommonConstant as Const } from '../common/Constant';
import { KvStoreModel } from '../model/KvStoreModel'
import promptAction from '@ohos.promptAction';
let kvStoreModel: KvStoreModel = new KvStoreModel()


export default class EntryFormAbility extends FormExtensionAbility {
  onAddForm(want) {

    // Called to return a FormBindingData object.
    let formData = {};
    return formBindingData.createFormBindingData(formData);
    // 获取数据库对象
    let context =  this.context; //getContext(this) as common.UIAbilityContext
    //let context = globalThis.context
    kvStoreModel.createKvStore(context,(value)=>{
      console.info('KVStore:kvStoreModel.createKvStore Callback'+value)
    })
    // 获取formID
    let formId = want.parameters["ohos.extra.param.key.form_identity"];
    let dataObj1 = {
      "formId": formId
    };
    let obj1 = formBindingData.createFormBindingData(dataObj1);
    return obj1;
  }

  onCastToNormalForm(formId) {
    // Called when the form provider is notified that a temporary form is successfully
    // converted to a normal form.
  }

  onUpdateForm(formId) {
    // 每30min自动刷新一次
    let formData = {
      'power': '99', // 和卡片布局中-电量对应
      'location': '上海', // 和卡片布局中-位置对应
      'distance': '123', // 和卡片布局中-里程对应
      'duration': '321', // 和卡片布局中-预计续航对应
      'beep': 'beep.', // 和卡片布局中-响铃找车对应
      'lock': 'lock', // 和卡片布局中-开锁对应
    };
    let formInfo = formBindingData.createFormBindingData(formData)
    formProvider.updateForm(formId, formInfo).then((data) => {
      console.info('FormAbility updateForm success.' + JSON.stringify(data));
    }).catch((error) => {
      console.error('FormAbility updateForm failed: ' + JSON.stringify(error));
    })

  }

  onChangeFormVisibility(newStatus) {
    // Called when the form provider receives form events from the system.
  }

  onFormEvent(formId, message) {

    // 手动点击卡片刷新具体数值
    let bike_power = ' ';
    let bike_location = ' ';
    let max_duration = ' ';
    let  bike_duration = ' ';
    let  bike_distance = ' ';
    // 获取数据库对象
    let context =  this.context; //getContext(this) as common.UIAbilityContext
    //let context = globalThis.context
    kvStoreModel.createKvStore(context,(value)=>{
      console.info('KVStore:kvStoreModel.createKvStore Callback'+value)
    })
    promptAction.showToast({'message':'获得骑行数据:'+bike_duration+bike_distance})

    // kvStoreModel.get(Const.PILOT_POWER_KEY,(value)=>
    // {
    //   bike_power = value
    // })
    // kvStoreModel.get(Const.PILOT_LOCATION_KEY,(value)=>
    // {
    //   bike_location = value
    // })
    // kvStoreModel.get(Const.PILOT_MAX_DURATION_KEY,(value)=>
    // {
    //   max_duration = '200'
    //   // 使用电量预估续航,公式: 剩余电量/总电量 = 预计续航/用户设定最大续航
    //   bike_duration = (parseInt(max_duration)*parseInt(bike_power)/100).toFixed(0)
    //   // 累计骑行 = 最大续航-预计续航
    //   bike_distance = (parseInt(max_duration) - parseInt(bike_duration)).toFixed(0)
    //   promptAction.showToast({'message':'获得骑行数据:'+bike_duration+bike_distance})
    // })
    bike_power = '49'
    max_duration = '1519'
    bike_location = '深圳-固定'
    // 使用电量预估续航,公式: 剩余电量/总电量 = 预计续航/用户设定最大续航
    bike_duration = (parseInt(max_duration)*parseInt(bike_power)/100).toFixed(0)
    // 累计骑行 = 最大续航-预计续航
    bike_distance = (parseInt(max_duration) - parseInt(bike_duration)).toFixed(0)
    promptAction.showToast({'message':'获得骑行数据:'+bike_duration+bike_distance})

    console.info(`FormAbility onEvent, formId = ${formId}, message: ${JSON.stringify(message)}`);
    let formData = {
      'power': bike_power, // 和卡片布局中-电量对应
      'location': bike_location, // 和卡片布局中-位置对应
      'distance': bike_distance, // 和卡片布局中-里程对应
      'duration': bike_duration, // 和卡片布局中-预计续航对应
      'beep': 'beep.', // 和卡片布局中-响铃找车对应
      'lock': 'lock', // 和卡片布局中-开锁对应
    };
    let formInfo = formBindingData.createFormBindingData(formData)
    formProvider.updateForm(formId, formInfo).then((data) => {
      console.info('FormAbility updateForm success.' + JSON.stringify(data));
    }).catch((error) => {
      console.error('FormAbility updateForm failed: ' + JSON.stringify(error));
    })

  }

  onRemoveForm(formId) {
    // Called to notify the form provider that a specified form has been destroyed.
  }

  onAcquireFormState(want) {
    // Called to return a {@link FormState} object.
    return formInfo.FormState.READY;
  }
};

